package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.OrdersAdminMapper;
import com.sky.mapper.OrdersDetailAdminMapper;
import com.sky.service.OrderAdminService;
import com.sky.vo.OrderDetailsVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderAdminServiceImpl implements OrderAdminService {
    @Autowired
    OrdersAdminMapper ordersAdminMapper;

    @Autowired
    OrdersDetailAdminMapper ordersDetailAdminMapper;

    @Override
    /**搜索订单*/
    public OrderVO OrderPage(OrdersPageQueryDTO ordersPageQueryDTO) {

        List<String> list ;
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
//获取符合条件订单数据
        Page<OrderDetailsVO> orderDetailList = ordersAdminMapper.OrderPage(ordersPageQueryDTO.getBeginTime()
                ,ordersPageQueryDTO.getEndTime()
                ,ordersPageQueryDTO.getStatus()
                ,ordersPageQueryDTO.getPhone()
                ,ordersPageQueryDTO.getNumber());

        for (OrderDetailsVO orderDetail : orderDetailList) {
            list =  ordersDetailAdminMapper.selectNameByOrderId(orderDetail.getId());

            String orderDishes ="";
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    orderDishes = orderDishes +"*"+ list.get(i);
                }
                orderDetail.setOrderDishes(orderDishes);
            }

        }
        OrderVO orderVO = new OrderVO();

        orderVO.setRecords(orderDetailList.getResult());

        orderVO.setTotal(orderDetailList.getTotal());

        return orderVO;
    }

    /**检索订单状态*/
    @Override
    public OrderStatisticsVO queryStatistics() {
//带派送状态数量 status = 3
        Integer confirmedNum = ordersAdminMapper.queryStatConfirmedNum();
//派送中状态status = 4
        Integer deliveryInProgressNum = ordersAdminMapper.queryStatDeliveryInProgressNum();
//待接单数量status = 2
        Integer toBeConfirmedNum = ordersAdminMapper.queryStatToBeConfirmedNum();

        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO(toBeConfirmedNum,confirmedNum,deliveryInProgressNum);

        return orderStatisticsVO;
    }

    //取消订单
    @Override
    @Transactional
    public void cancelOrder(OrdersCancelDTO ordersCancelDTO) {
        if(ordersCancelDTO == null){throw new DeletionNotAllowedException(MessageConstant.SELECT_NOTHING);
        }

        //根据id查看订单状态是否是待派送状态 status = 3 或者派送中状态status = 4
        Integer cancelStatusNum = ordersAdminMapper.queryCancelStatus(ordersCancelDTO);

        if(cancelStatusNum == null){ throw new DeletionNotAllowedException(MessageConstant.ORDER_STATUS_ERROR);}

        //将取消订单的信息存入
        ordersAdminMapper.updateCancelOrderMS(ordersCancelDTO.getCancelReason(),ordersCancelDTO.getId(), LocalDateTime.now());

        //将订单状态改变
        ordersAdminMapper.upateOrderStatus(ordersCancelDTO.getId());
    }

    /**完成订单*/
    @Override
    public void completeOrder(Long id) {
        if (id == null) {throw new DeletionNotAllowedException(MessageConstant.ID_IS_NULL);}

        //将订单状态改变
        ordersAdminMapper.upateOrdercompleteStatus(id,LocalDateTime.now());

    }

    /**拒绝订单*/
    @Override
    public void rejectionOrder(OrdersRejectionDTO rejectionDTO) {
        if(rejectionDTO == null){throw new DeletionNotAllowedException(MessageConstant.SELECT_NOTHING);}

        //根据id查看订单状态是否是待接单状态 status = 2
        int status = 2;
        Integer rejectStatusNum = ordersAdminMapper.queryOrderStatus(rejectionDTO.getId(),status);

        if(rejectStatusNum == null){ throw new DeletionNotAllowedException(MessageConstant.ORDER_STATUS_ERROR);}

        //将取消订单的信息存入
        ordersAdminMapper.updateRejectionOrderMS(rejectionDTO.getRejectionReason(),rejectionDTO.getId(),LocalDateTime.now());

        //将订单状态改变
        ordersAdminMapper.upateOrderRejectionStatus(rejectionDTO.getId());

    }

/**接受订单*/
    @Override
    public void confirmOrder(Long id) {
        //判空
        if(id == null){throw new DeletionNotAllowedException(MessageConstant.ID_IS_NULL);}

        //根据id查看订单状态是否是待接单状态 status = 2
        int status = 2;
        Integer rejectStatusNum = ordersAdminMapper.queryOrderStatus((long)id,status);

        if(rejectStatusNum == null){ throw new DeletionNotAllowedException(MessageConstant.ORDER_STATUS_ERROR);}

        //将订单状态改变
        ordersAdminMapper.upateOrderconfirmStatus(id);

    }

    /**派送订单*/
    @Override
    public void deliveryOrder(Integer id) {
        //判空
        if(id == null){throw new DeletionNotAllowedException(MessageConstant.ID_IS_NULL);}

        //根据id查看订单状态是否是待接单状态 status = 3
        int status = 3;
        Integer rejectStatusNum = ordersAdminMapper.queryOrderStatus((long)id,status);

        if(rejectStatusNum == null){ throw new DeletionNotAllowedException(MessageConstant.ORDER_STATUS_ERROR);}


        ordersAdminMapper.deliveryOrder(id);
    }
}
